﻿using System;
using System.ComponentModel;
using System.Reflection;
using System.Windows.Forms;

namespace StockUI
{
    [DesignTimeVisible(false)]
    public class FormAbstractParent : FormAbstract
    {
        private FormAbstractChild activeChild = null;

        private Panel pnlContainer = null;

        public FormAbstractParent()
        {
            pnlContainer = new Panel();
            pnlContainer.Dock = DockStyle.Fill;
            this.Controls.Add(pnlContainer);

            this.Load += new EventHandler(FormAbstractParent_Load);
        }

        void FormAbstractParent_Load(object sender, EventArgs e)
        {
            pnlContainer.BringToFront();
        }

        public FormAbstractChild ActiveFormChild
        {
            get
            {
                return this.activeChild;
            }
            set
            {
                if (value != null && this.activeChild != value)
                {
                    this.activeChild = value;
                    value.BringToFront();
                    value.Focus();
                }
            }
        }

        public FormAbstractChild FindFormChild(string name)
        {
            return FindFormChild(name, true, true);
        }

        private FormAbstractChild FindFormChild(string name, bool create, bool activate)
        {
            FormAbstractChild form = null;
            foreach (Control ctrl in pnlContainer.Controls)
            {
                if (ctrl is FormAbstractChild && ctrl.Name == name)
                {
                    form = ctrl as FormAbstractChild;
                    break;
                }
            }
            if (form == null && create)
            {
                try
                {
                    string className = string.Format("{0}.Form{1}", this.GetType().Namespace, name);
                    object instance = Assembly.GetExecutingAssembly().CreateInstance(className);
                    if (!(instance is FormAbstractChild))
                    {
                        throw new Exception("子窗体不是继承自FormChild");
                    }

                    form = instance as FormAbstractChild;
                    form.Name = name;
                    form.TopLevel = false;
                    form.Parent = pnlContainer;
                    form.Visible = true;
                    form.Focus();
                    form.Activate();
                }
                catch (Exception ex)
                {
                    throw new Exception("动态创建子窗体实例失败！" + ex.Message);
                }
            }

            if (form != null && activate)
            {
                this.ActiveFormChild = form;
            }

            return form;
        }

        //切换子窗口显示
        private FormAbstractChild SwitchFormChild(bool forword)
        {
            int activeIndex = -1;
            for (int i = 0; i < MdiChildren.Length; i++)
            {
                if (MdiChildren[i] == ActiveMdiChild)
                {
                    activeIndex = i;
                    break;
                }
            }

            if (activeIndex >= 0)
            {
                activeIndex = (activeIndex + MdiChildren.Length + (forword ? 1 : -1)) % MdiChildren.Length;
                FormAbstractChild child = MdiChildren[activeIndex] as FormAbstractChild;
                //child.Activate();
                SendKeys.Send(forword ? "^{TAB}" : "^+{TAB}");
                return child;
            }

            return null;
        }
    }
}
